package vincent.com.layouttest.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import vincent.com.layouttest.R;

/**
 * Created by chenzhengwen on 2015/5/13.
 */
public class CircleColorView extends View {
    private final String TAG = "CircleColorView";
    private final int DEFAULT_SIZE = 100;
    private final int DEFAULT_CIRCLE_COLOR = getResources().getColor(android.R.color.holo_blue_light);

    private Paint mCirclePaint;
    private int mCircleColor;
    private float mRadius;

    public CircleColorView(Context context) {
        this(context, null);
    }

    public CircleColorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);

    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr){
        mCirclePaint = new Paint();
        int txtSize = (int)(20 * getResources().getDisplayMetrics().density);
        mCirclePaint.setTextSize(txtSize);

        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CircleColor);

        mCircleColor = attributes.getColor(R.styleable.CircleColor_circle_color, DEFAULT_CIRCLE_COLOR);
        mRadius = attributes.getFloat(R.styleable.CircleColor_radius, DEFAULT_SIZE/2);
        attributes.recycle();
        mCirclePaint.setColor(mCircleColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        int defaultSize = (int)(mRadius * 2 * getContext().getResources().getDisplayMetrics().density);
        int width = defaultSize;
        int height = defaultSize;

        if(widthSpecMode == MeasureSpec.EXACTLY){
            width = widthSpecSize;
        }else if(widthSpecMode == MeasureSpec.AT_MOST){
            width = Math.min(width, widthSpecSize);
        }

        if(heightSpecMode == MeasureSpec.EXACTLY){
            height = heightSpecSize;
        }else if(heightSpecMode == MeasureSpec.AT_MOST){
            height = Math.min(height, heightSpecSize);
        }

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float x = getMeasuredWidth()/2;
        float y = getMeasuredHeight()/2;
        float radius = mRadius * getResources().getDisplayMetrics().density;
        canvas.save();
        Log.i(TAG, "x=" + x + " | y=" + y + " | radius=" + radius);
        canvas.drawCircle(x, y, radius, mCirclePaint);
        canvas.restore();
    }

    public void setCircleColor(int color){

        mCircleColor = color;
        mCirclePaint.setColor(mCircleColor);
        invalidate();
    }

    public void setRadius(float radius){
        if(radius < 0){
            return;
        }

        mRadius = radius;
        invalidate();
    }
}
